package newtest;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.io.IOUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.StatusLine;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicHeader;
import org.apache.http.protocol.HTTP;
import org.apache.http.ssl.SSLContextBuilder;

import javax.net.ssl.SSLContext;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;

public class NewSalesDemo {

    static String httpProtocol = "http";
    static String username = "";//中银保信提供的对接账号
    static String password = "";//中银保信提供的对接账号的密码
    static String ip = "";//中银保信双录服务器IP地址
    static int port = 0;//中银保信双录服务器端口号

    static HttpHost target = null;

    public static void main(String[] args) throws Exception {
        JSONArray userArray = new JSONArray();
        JSONObject userObject = new JSONObject();
        userObject.put("unitCode", "0002");//营销员所属机构编码
        userObject.put("telPhone", "15012740318");//营销员手机号码
        userObject.put("name", "XXX");//营销员所姓名
        userObject.put("gender", "1");//营销员性别
        userObject.put("certification", "12345678901234567890234343");//执业证号
        userObject.put("workNumber", "");//工号
        userObject.put("idCardType", "2");//证件类型
        userObject.put("idCardNo", "123456");//证件号码
        userArray.add(userObject);
        createUser(userArray);
    }

    private static void createUser(JSONArray userArray) throws Exception {
        CloseableHttpClient httpclient = createSSLClientDefault();
        try {
            HttpClientContext localContext = HttpClientContext.create();
            HttpPost httpPost = new HttpPost(httpProtocol + "://" + ip + ":" + port + "/join/createSalesmanBatch");

            String body = JSON.toJSONString(userArray);
            httpPost.setHeader("Content-Type", "application/json");
            StringEntity stringEntity = new StringEntity(body, "UTF-8");// 解决中文乱码问题
            stringEntity.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json;charset=utf8"));
            httpPost.setEntity(stringEntity);
            CloseableHttpResponse response = httpclient.execute(target, httpPost, localContext);
            try {
                System.out.println("------------------createSalesmanBatch----------------------");
                StatusLine tLine = response.getStatusLine();
                System.out.println(tLine);
                HttpEntity responseEntity = response.getEntity();
                String data = IOUtils.toString(responseEntity.getContent(), "UTF-8");
                System.out.println(data);
            } finally {
                response.close();
            }
        } finally {
            httpclient.close();
        }
    }

    static CloseableHttpClient httpSSlClient = null;

    /**
     * 获取受信任httpClient
     *
     * @return
     */
    public static CloseableHttpClient createSSLClientDefault() {
        if (target == null)
            target = new HttpHost(ip, port, httpProtocol);
        if (httpSSlClient != null)
            return httpSSlClient;
        CredentialsProvider credsProvider = new BasicCredentialsProvider();
        credsProvider.setCredentials(new AuthScope(target.getHostName(), target.getPort()),
                new UsernamePasswordCredentials(username, GetMD5.GetMD5Code(password)));
        SSLContext sslContext;

        try {
            sslContext = new SSLContextBuilder().loadTrustMaterial(null, (certificate, authType) -> true).build();
            SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext, new String[] { "TLSv1.2" },
                    null, new NoopHostnameVerifier());
            httpSSlClient = HttpClients.custom().setSSLSocketFactory(sslsf).setDefaultCredentialsProvider(credsProvider)
                    .build();
            return httpSSlClient;
        } catch (KeyManagementException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (KeyStoreException e) {
            e.printStackTrace();
        }
        return null;
    }

}
